package yawning.mew.bear;

import yawning.mew.bear.BearThreatSimulation.SimulatorProperty;
import yawning.mew.bear.BearThreatSimulator.BearTargetData;
import yawning.mew.character.CharacterModel.AttackResult;
import yawning.mew.sim.SimulationEventAction;

public class FeralFFAction extends SimulationEventAction {
	BearThreatSimulator mState;
	BearTargetData mTargetData;
	boolean mExternal;
	int mStacks = 0;

	FeralFFAction(BearThreatSimulator state, BearTargetData targetData) {
		super(state.mSimEngine);
		mState = state;
		mTargetData = targetData;
		mExternal = mState.mSimEngine.getConfig().getBool(SimulatorProperty.ASSUMEEXTERNALFF);
		if (mExternal) {
			mStacks = 3;
			schedule(Double.MAX_VALUE);

			if (mState.mIsLogging) mState.mLog.log("Feral FF: External\n");
		}

		mTargetData.setArmorMajorMult(1 - 0.04 * mStacks);
	}

	@Override
	public double perform() {
		AttackResult result = mState.mModel.getSpellResult();

		if (result == AttackResult.MISS) {
			mNrFFMiss++;

			if (mState.mIsLogging) mState.mLog.log("Feral FF: MISS Stacks: %d\n", mStacks);
		} else {
			double damage = mState.mModel.mLevelDep.getFeralFFBaseDmg() + mState.mModel.getAttackPower() * mState.mModel.mLevelDep.getFeralFFCoeff();

			if (result == AttackResult.CRIT) {
				mNrFFCrit++;
				damage *= mState.mModel.multSpellCrit;
			} else {
				mNrFFHit++;
			}

			damage *= mState.getSpellDamageMult();

			mFFDamage += damage;

			if (mState.mIsLogging) mState.mLog.log("Feral FF: %s Damage: %d Stacks: %d\n", result, Math.round(damage), mStacks);

			if (!mExternal) {
				// Recalculate armor here.
				mStacks = Math.min(3, mStacks + mState.mModel.nrFeralFFStacksPerCast);
				mTargetData.setArmorMajorMult(1 - 0.04 * mStacks);
				schedule(mState.mModel.feralFFDebuffDuration);
			}
		}

		return 1.0;
	}

	@Override
	public void onExpire() {
		if (mState.mIsLogging) mState.mLog.log("Feral FF Expiration\n");

		mStacks = 0;
		mTargetData.setArmorMajorMult(1 - 0.04 * mStacks);
	}

	long mFFDamage = 0;
	int mNrFFMiss = 0;
	int mNrFFHit = 0;
	int mNrFFCrit = 0;
}